package com.nm.factory;

import com.nm.dto.DivideLendInfo;
import com.nm.dto.MonthPaybackInfo;
import com.nm.dto.SeperateFee;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;

public class LenderToolBox {

    public static void main(String[] args) {
        LenderToolBox lenderToolBox = new LenderToolBox();
        DivideLendInfo info = lenderToolBox.everyMonthSamePayBack(LocalDate.now(), true, 10000, 12, 0.36, 1000, 2);
        System.out.println(info);
    }


    /**
     * 等额本息,每一期，一个月，30天
     *
     * @param print
     * @param ben_jin
     * @param fee
     * @param qishu
     * @param rate
     */
    public DivideLendInfo everyMonthSamePayBack(LocalDate lendTime, boolean print, double ben_jin, double qishu, double rate, double fee, int qishuOfFee) {
        DivideLendInfo divideLendInfo = new DivideLendInfo();
        divideLendInfo.setYearRate(new BigDecimal(rate));
        divideLendInfo.setPrincipal(new BigDecimal(ben_jin));
        double rateOfMonth = rate / 12;//
        divideLendInfo.setMonthRate(new BigDecimal(rateOfMonth));
        double everyMonthPayBack = ben_jin * rateOfMonth * Math.pow(1 + rateOfMonth, qishu) / (Math.pow(1 + rateOfMonth, qishu) - 1);
        divideLendInfo.setPaybackAmountEverySequence(new BigDecimal(everyMonthPayBack));
        double yinli = everyMonthPayBack * qishu - ben_jin;
        double irr = (yinli + fee) / ben_jin;
        if (print) {
            System.out.printf("本金 %.2f 期数 %.0f 利率(APR) %.6f 利息盈利 %.2f 手续费 %.2f 真实利率(IRR) %.6f\n", ben_jin, qishu, rate, yinli, fee, irr);
        }
        List<MonthPaybackInfo> monthPaybackInfos = new LinkedList<>();
        for (int i = 1; i <= qishu; i++) {
            double currrentInterest = ben_jin * rateOfMonth * (Math.pow(1 + rateOfMonth, qishu) - Math.pow(1 + rateOfMonth, i - 1)) / (Math.pow(1 + rateOfMonth, qishu) - 1);

            MonthPaybackInfo monthPaybackInfo = new MonthPaybackInfo();
            monthPaybackInfo.setAmountPayback(new BigDecimal(everyMonthPayBack));
            monthPaybackInfo.setMonthSequeuence(i);
            monthPaybackInfo.setInterestFee(new BigDecimal(currrentInterest));
            monthPaybackInfo.setPrincipal(monthPaybackInfo.getAmountPayback().subtract(monthPaybackInfo.getInterestFee()));
            LocalDate newDate = lendTime.plusMonths(i);
            monthPaybackInfo.setDatetime(newDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
            monthPaybackInfo.setTimestamp(System.currentTimeMillis());
            if (print) {
                System.out.printf("第%d期 %s %.2f %.2f %.2f\n", i,monthPaybackInfo.getDatetime(), everyMonthPayBack, currrentInterest, everyMonthPayBack - currrentInterest);
            }
            if (i <= qishuOfFee) {
                SeperateFee seperateFee = new SeperateFee();
                seperateFee.setAmount(new BigDecimal(fee).divide(new BigDecimal(qishuOfFee)));
                seperateFee.setComment("手续费");
                List<SeperateFee> seperateFees = new LinkedList<>();
                seperateFees.add(seperateFee);
                monthPaybackInfo.setSeperateFees(seperateFees);
            }

            monthPaybackInfos.add(monthPaybackInfo);
        }
        divideLendInfo.setMonthPaybackInfos(monthPaybackInfos);
        divideLendInfo.setRevenue(new BigDecimal(everyMonthPayBack * qishu + fee));
        return divideLendInfo;
    }
}
